#include <stdlib.h>
#include <gtk/gtk.h>

void
destroy_cb(GtkWidget *widget,gpointer data) {
	g_debug("destroy_cb");
	gtk_main_quit();
}
void
realize_cb(GtkWidget *widget,gpointer data) {
	g_debug("realize_cb");
}
gboolean
configure_event_cb(GtkWidget *widget,GdkEventConfigure *conf,gpointer data) {
	static gint x=-1;
	static gint y=-1;
	static gint width=-1;
	static gint height=-1;
	g_debug("configure_event_cb");
	if (conf->x!=x || conf->y!=y) {
		x=conf->x;
		y=conf->y;
		g_debug("widget moved to %d %d",x,y);
	}
	if (conf->width!=width || conf->height!=height) {
		width=conf->width;
		height=conf->height;
		g_debug("widget resized to %d %d",width,height);
	}
	return FALSE;
}
void
greencross(cairo_t *cr,guint width,guint height) {
	g_debug("greencross");
	// зелёное перекрестие
	cairo_set_source_rgb(cr,0,180,0);
	cairo_move_to(cr,0,0);
	cairo_line_to(cr,width-1,height-1);
	cairo_move_to(cr,0,height-1);
	cairo_line_to(cr,width-1,0);
	cairo_stroke(cr);
}
#if (GTK_MAJOR_VERSION == 3)
gboolean
draw_cb(GtkWidget *widget,cairo_t *cr,gpointer data) {
	guint width,height;
	cairo_path_t *path;
	g_debug("draw_cb");
	width=gtk_widget_get_allocated_width(widget);
	height=gtk_widget_get_allocated_height(widget);
	greencross(cr,width,height);
	return FALSE;
}
#else
gboolean
expose_event_cb(GtkWidget *widget,GdkEvent *event,gpointer data) {
	GdkWindow *window;
	cairo_t *cr;
	g_debug("expose_event_cb");
	window=gtk_widget_get_window(widget);
	if (window==NULL) {
		g_error("!gtk_widget_get_window");
		exit(1);
	}
	cr=gdk_cairo_create(GDK_DRAWABLE(window));
	greencross(cr,gdk_window_get_width(window),gdk_window_get_height(window));
}
#endif
int
main(int argc,char *argv[]) {
	GtkWidget *window;
	GtkWidget *area;

	gtk_init(&argc,&argv);

	window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
	area=gtk_drawing_area_new();

	gtk_container_add(GTK_CONTAINER(window),area);

	g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(destroy_cb),NULL);
#if (GTK_MAJOR_VERSION == 3)
	g_signal_connect(G_OBJECT(area),"draw",G_CALLBACK(draw_cb),NULL);
#else
	g_signal_connect(G_OBJECT(area),"expose-event",G_CALLBACK(expose_event_cb),NULL);
#endif
	g_signal_connect(G_OBJECT(area),"realize",G_CALLBACK(realize_cb),NULL);
	g_signal_connect(G_OBJECT(area),"configure-event",G_CALLBACK(configure_event_cb),NULL);
	
	gtk_widget_show_all(window);

	gtk_main();

	return 0;
}

